<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class fakultas extends CI_Controller {

	function __construct(){
            parent::__construct();
            $this->load->library(array('session','form_validation'));
            $this->load->helper(array('form', 'url'));
            $this->admin_id = $this->session->userdata('admin_id');
            $this->admin_username = $this->session->userdata('admin_username');
            $this->load->model('fakultas_model');
        }
        
        public function add_fakultas()
	{
            if($this->admin_id)
            {
                $nama = $this->input->post('nama');
                $singkatan = $this->input->post('singkatan');
                $alamat = $this->input->post('alamat');
                $telp = $this->input->post('telp');
                $fax = $this->input->post('fax');
                $email = $this->input->post('email');
                $website = $this->input->post('website');

                $data['submit_data']=array(
                    'nama' => $this->input->post('nama'),
                    'singkatan' => $this->input->post('singkatan'),
                    'alamat' => $this->input->post('alamat'),
                    'telp' => $this->input->post('telp'),
                    'fax' => $this->input->post('fax'),
                    'email' => $this->input->post('email'),
                    'website' => $this->input->post('website'),
                );

                $this->form_validation->set_message('required', "%s wajib di isi");
                $this->form_validation->set_message('min_length', "%s harus lebih dari 6 karakter");
                $this->form_validation->set_message('max_length', "%s tidak boleh lebih dari 12 karakter");
                $this->form_validation->set_message('valid_email', "%s tidak valid");
                $this->form_validation->set_message('numeric', "%s tidak valid, isikan nomor yang benar");

                $this->form_validation->set_rules('nama', 'Nama', 'trim|required');
                $this->form_validation->set_rules('singkatan', 'Singkatan Fakultas', 'trim|required');
                $this->form_validation->set_rules('email', 'Email', 'trim|valid_email|required');
                $this->form_validation->set_rules('telp', 'Nomor Telepon', 'trim|required|numeric');
                $this->form_validation->set_rules('alamat', 'Alamat', 'trim|required');
                $this->form_validation->set_rules('fax', 'No. Fax', 'trim|required|numeric');
                $this->form_validation->set_rules('website', 'Alamat Website', 'trim|required');

                if ($this->form_validation->run() == FALSE)
                {
                    $this->load->view('template/header', $data); 
                    $this->load->view('fakultas/add_fakultas', $data);
                    $this->load->view('admin/sidebar', $data);
                    $this->load->view('template/footer', $data);
                }
                else
                {
                    $this->fakultas_model->insert_fakultas($nama, $singkatan, $alamat, $telp, $fax, $email, $website);
                    $this->load->view('template/header'); 
                    $this->load->view('fakultas/register_finished');
                    $this->load->view('admin/sidebar', $data);
                    $this->load->view('template/footer');
                }
            }
            else
            {
                redirect('admin');
            }
            
	}
        
        public function edit_fakultas()
	{
            if($this->admin_id)
            {
                $fakultas_id = $this->input->post('fakultas_id');
                $nama = $this->input->post('nama');
                $singkatan = $this->input->post('singkatan');
                $alamat = $this->input->post('alamat');
                $telp = $this->input->post('telp');
                $fax = $this->input->post('fax');
                $email = $this->input->post('email');
                $website = $this->input->post('website');

                $data['data_fakultas']=array(
                    'nama' => $this->input->post('nama'),
                    'singkatan' => $this->input->post('singkatan'),
                    'alamat' => $this->input->post('alamat'),
                    'telp' => $this->input->post('telp'),
                    'fax' => $this->input->post('fax'),
                    'email' => $this->input->post('email'),
                    'website' => $this->input->post('website'),
                );


                $data["data_fakultas"] = $this->fakultas_model->get_fakultas_byid($fakultas_id);
                $this->form_validation->set_message('required', "%s wajib di isi");
                $this->form_validation->set_message('min_length', "%s harus lebih dari 6 karakter");
                $this->form_validation->set_message('max_length', "%s tidak boleh lebih dari 12 karakter");
                $this->form_validation->set_message('valid_email', "%s tidak valid");
                $this->form_validation->set_message('numeric', "%s tidak valid, isikan nomor yang benar");

                $this->form_validation->set_rules('nama', 'Nama', 'trim|required');
                $this->form_validation->set_rules('singkatan', 'Singkatan Fakultas', 'trim|required');
                $this->form_validation->set_rules('email', 'Email', 'trim|valid_email|required');
                $this->form_validation->set_rules('telp', 'Nomor Telepon', 'trim|required|numeric');
                $this->form_validation->set_rules('alamat', 'Alamat', 'trim|required');
                $this->form_validation->set_rules('fax', 'No. Fax', 'trim|required|numeric');
                $this->form_validation->set_rules('website', 'Alamat Website', 'trim|required');

                if ($this->form_validation->run() == FALSE)
                {
                    $this->load->view('template/header', $data); 
                    $this->load->view('fakultas/edit_fakultas', $data);
                    $this->load->view('admin/sidebar', $data);
                    $this->load->view('template/footer', $data);
                }
                else
                {
                    $this->fakultas_model->edit_fakultas($fakultas_id, $nama, $singkatan, $alamat, $telp, $fax, $email, $website);
                    $this->load->view('template/header'); 
                    $this->load->view('fakultas/update_finished');
                    $this->load->view('admin/sidebar', $data);
                    $this->load->view('template/footer');
                }
            }
            else
            {
                redirect('admin');
            }
	}
        
        public function view_fakultas()
	{
            if($this->admin_id)
            {
                $fakultas_id = $this->input->post('fakultas_id');
                $data["data_fakultas"] = $this->fakultas_model->get_fakultas_byid($fakultas_id);

                $this->load->view('template/header', $data); 
                $this->load->view('fakultas/view_fakultas', $data);
                $this->load->view('admin/sidebar', $data);
                $this->load->view('template/footer', $data);
            }
            else
            {
                redirect('admin');
            }
        }
        
        public function del_fakultas()
	{
            if($this->admin_id)
            {
                $fakultas_id = $this->input->post('fakultas_id');
                $this->fakultas_model->del_fakultas($fakultas_id);
                redirect('admin');
            }
            else
            {
                redirect('admin');
            }
        }
}